﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RAMED
{
    public partial class RechercheDossierArchiveForm : Form
    {
        public List<Dossier_Archive> RechercheDossierArchive = new List<Dossier_Archive>();
        RAMEDEntities1 context = SingletonContext.GetContext();

        public RechercheDossierArchiveForm()
        {
            InitializeComponent();
            comboBoxannexe.DataSource = context.Annexe_administratif;
            comboBoxannexe.DisplayMember = "nom_annexe_administratif";
            comboBoxannexe.SelectedItem = null;
       }

        private void remplirDataGridView()
        {

            dataGridViewdossierarchive.AutoGenerateColumns = false;
            dataGridViewdossierarchive.ColumnCount = 6;

            dataGridViewdossierarchive.Columns[0].HeaderText = "Nom";
            dataGridViewdossierarchive.Columns[0].DataPropertyName = "Nom";

            dataGridViewdossierarchive.Columns[1].HeaderText = "Prénom";
            dataGridViewdossierarchive.Columns[1].DataPropertyName = "prénom";


            dataGridViewdossierarchive.Columns[2].HeaderText = "Num de Dossier";
            dataGridViewdossierarchive.Columns[2].DataPropertyName = "Num_Dossier";

            dataGridViewdossierarchive.Columns[3].HeaderText = "Cin";
            dataGridViewdossierarchive.Columns[3].DataPropertyName = "Cin";

            dataGridViewdossierarchive.Columns[4].HeaderText = "Date Archive";
            dataGridViewdossierarchive.Columns[4].DataPropertyName = "date_archive";

            dataGridViewdossierarchive.Columns[5].HeaderText = "Annexe Administratif";
            dataGridViewdossierarchive.Columns[5].DataPropertyName = "Annexe_administratif";

            dataGridViewdossierarchive.DataSource = RechercheDossierArchive;
        }

        public void mettreAjourFormGridView()
        {
            dataGridViewdossierarchive.DataSource = null;
            remplirDataGridView();
            dataGridViewdossierarchive.Update();
            dataGridViewdossierarchive.Refresh();
        }
        private void btnrechercher_Click(object sender, EventArgs e)
        {
           
        }

        private void btnmodifier_Click(object sender, EventArgs e)
        {

        }

        private void btnmodifier_Click_1(object sender, EventArgs e)
        {
            if (dataGridViewdossierarchive.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner un enregistrement pour le modifier", Color.Red, 3000);
            }
            else
            {

                ModifierDossierArchiveForm MDA = new ModifierDossierArchiveForm();


                Dossier_Archive Dossier = ((Dossier_Archive)dataGridViewdossierarchive.CurrentRow.DataBoundItem);

                MDA.Dossier = Dossier;
                MDA.recherchedossierArchive = this;
                MDA.InitialiserChamps();
                MDA.Show();
            }
        }

        private void btnrechercher_Click_1(object sender, EventArgs e)
        {
            RAMEDEntities1 context = SingletonContext.GetContext();
            List<Dossier_Archive> LesDossierarchives = new List<Dossier_Archive>();
            LesDossierarchives = context.Dossier_Archive.ToList<Dossier_Archive>();

            if (comboBoxannexe.SelectedItem != null)
            {
                Annexe_administratif annexeadministrative = new Annexe_administratif();
                annexeadministrative = (Annexe_administratif)comboBoxannexe.SelectedValue;
                LesDossierarchives = LesDossierarchives.Where(p => p.id_annexe_administratif == annexeadministrative.id_annexe_administratif).ToList<Dossier_Archive>();

            }
            if (!txtcin.Text.Trim().Equals(""))
            {


                LesDossierarchives = LesDossierarchives.Where(p => p.CIN.Equals(txtcin.Text)).ToList<Dossier_Archive>();

            }
            if (!txtnom.Text.Trim().Equals(""))
            {


                LesDossierarchives = LesDossierarchives.Where(p => p.Nom.ToUpper().Equals(txtnom.Text.ToUpper())).ToList<Dossier_Archive>();

            }
            if (!txtprenom.Text.Trim().Equals(""))
            {

                LesDossierarchives = LesDossierarchives.Where(p => p.Prénom.ToUpper().Equals(txtprenom.Text.ToUpper())).ToList<Dossier_Archive>();

            }

            if (!txtnumdoss.Text.Trim().Equals(""))
            {

                LesDossierarchives = LesDossierarchives.Where(p => p.Num_Dossier.ToUpper().Equals(txtnumdoss.Text.ToUpper())).ToList<Dossier_Archive>();
            }

            if (checkBoxdatedeclaration.Checked)
            {

                LesDossierarchives = LesDossierarchives.Where(c => c.date_archive.Value.Date >= dateTimedeclaration.Value.Date).ToList();
            }

              RechercheDossierArchive = LesDossierarchives;
              mettreAjourFormGridView();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
